package com.feeyo.monitor.dao;

import java.lang.reflect.Array;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.feeyo.monitor.schedule.ArchiverTaskImp;
import org.influxdb.dto.Point;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;
import org.influxdb.dto.QueryResult.Series;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.influxdb.InfluxDBTemplate;
import org.springframework.stereotype.Component;

/**
 * influxdb访问类
 * @Author <a href="mailto:bliulei@variflight.com">liulei</a>
 * @Description Created by liulei on 2017/5/18 下午5:42.
 */
@Component
public class InfluxDaoImp implements InfluxDao {
	@Autowired
	InfluxDBTemplate<Point> influxDBTemplate;
    private static String dbName = "vehiclepos";
    private static String measure = "pos";

    Logger logger = LoggerFactory.getLogger(InfluxDaoImp.class);

	@Override
	public List getTrackingByVehicle(String devid,String startTime,String endTime){
		//Query query = new Query("select lon,lat,speed from pos_2 where time > '2017-04-26T10:19:16Z' and time < '2017-04-26T10:27:48Z'", "fp_test");
		Query query = new Query("select lon,lat,speed,direct from " + measure + " where time > '"+startTime+"' and time < '"+endTime+"' and devid = '"+devid+"'", dbName);
        logger.info("sql->"+"select lon,lat,speed from " + measure + " where time > '"+startTime+"' and time < '"+endTime+"' and devid = '"+devid+"'");
		QueryResult result = influxDBTemplate.query(query);
        List<Series> seriesResult = result.getResults().get(0).getSeries();
        if ((seriesResult == null) || (seriesResult.size() == 0))
        	return null;
        QueryResult.Series re2 = result.getResults().get(0).getSeries().get(0);
        List re3 = re2.getValues();
		return re3;
	}

    @Override
    public Boolean ifOnline(String devid) {
        Query query = new Query("select lon,lat,speed from " + measure + " where  devid = '"+devid+"' limit 1", dbName);
        QueryResult result = influxDBTemplate.query(query);
        List<Series> seriesResult = result.getResults().get(0).getSeries();
        if ((seriesResult == null) || (seriesResult.size() == 0))
            return false;
        else
            return true;
    }
}
